<template>
  <div class="addCorp">
    <div class="table-header">
      <p>查看公司信息</p>
    </div>
    <table class="table">
      <thead>
        <tr>
          <th>公司名</th>
          <th>法人</th>
          <th>注册资金</th>
          <th>管理员手机号</th>
          <th>最后修改时间</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="corp of corpInfo" :key="corp.id">
          <td>{{ corp.name }}</td>
          <td>{{ corp.legal }}</td>
          <td>{{ corp.fund }}</td>
          <td>{{ corp.tel }}</td>
          <td v-if="corp.finalModifyTime">{{ corp.finalModifyTime }}</td>
          <td v-else>暂未修改</td>
        </tr>
      </tbody>
    </table>
    <footer>
      <Pages
        @setCurPage="setCurPage"
        :curPage="curPage"
        :total="total"
        :pageSize="pageSize"></Pages>
    </footer>
  </div>
</template>
<script>
import Pages from '@/components/Pages/index';
import api from '@/api/corpInfo';

export default {
  name: 'AddCorp',
  beforeRouteEnter(to, from, next) {
    if (sessionStorage.getItem('loginType') !== '4') {
      next({ path: '/login' });
    }
    next((vm) => {
      vm.$emit('route-switch', window.location.href.split('/').pop());
    });
  },
  data() {
    return {
      corpInfo: [
        {
          id: 'corp1',
          name: '洪湖里1',
          legal: '一心',
          fund: '10000人民币',
          tel: '17600000000',
          finalModifyTime: '2019-01-01 00:00:00',
        },
        {
          id: 'corp2',
          name: '洪湖里2',
          legal: '一心',
          fund: '10000人民币',
          tel: '17600000000',
          finalModifyTime: '2019-01-01 00:00:00',
        },
        {
          id: 'corp3',
          name: '洪湖里3',
          legal: '一心',
          fund: '10000人民币',
          tel: '17600000000',
          finalModifyTime: '2019-01-01 00:00:00',
        },
        {
          id: 'corp4',
          name: '洪湖里4',
          legal: '一心',
          fund: '10000人民币',
          tel: '17600000000',
          finalModifyTime: '2019-01-01 00:00:00',
        },
      ],
      curPage: 1,
      total: 100,
      pageSize: 10,
    };
  },
  methods: {
    setCurPage(page) {
      // TODO 换页刷新
      this.curPage = page;
      this.getCompanyList();
    },
    getCompanyList() {
      const params = new URLSearchParams();
      params.append('rows', this.pageSize);
      params.append('page', this.curPage);
      api.getCompanyList(params).then(async (response) => {
        this.corpInfo = [];
        const datas = response.data.rows;
        this.total = response.data.total;
        for (let i = 0; i < datas.length; i += 1) {
          this.corpInfo.push({
            id: datas[i].businessId,
            name: datas[i].name,
            legal: datas[i].legalPerson,
            fund: datas[i].capital,
            tel: datas[i].phoneNum,
            finalModifyTime: datas[i].updateDate,
          });
        }
      });
    },
  },
  mounted() {
    this.getCompanyList();
  },
  components: {
    Pages,
  },
};
</script>
<style lang="less">
@import url(../../../static/style/site.vars.less);
.addCorp {
  footer {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px 0;
    background: #fff;
  }
}
</style>
